package com.example.community.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: huang
 * @Date: 2021/9/18 21:30
 * @Description:    我们拦截器每次请求都要获得cookie进行判断，所以我们写个util进行复用
 */
public class CookieUtil {

    //传入request和我们想要的cookie的值。首先判断空值的情况，然后遍历cookie，找到我们想要的那个。
    public static String getValue(HttpServletRequest request, String name) {
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空!");
        }

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }

        return null;
    }

}
